/**
 * @ClassName: AboutFragment
 * @Description: 关于页面
 * @author Mengting Lu
 * @date 2015年7月31日 下午1:58:35
 */

package com.shanghaionstar.discover.charge;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ToggleButton;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BaseFragment;
import com.onstar.cn.weight.ProgressLayout;
import com.onstar.cn.weight.ToastView;
import com.shanghaionstar.R;
import com.shanghaionstar.discover.charge.iview.IChargeModeNotificationView;
import com.shanghaionstar.discover.charge.presenter.ChargeModeNotificationPresenter;
import com.shanghaionstar.weight.TopLayout;

/**
 * @author Kai Zhang
 * @ClassName: ChargeModeNotificationFragment
 * @Description: 充电模式提醒设置
 * @date 2015年12月7日
 */

public class ChargeModeNotificationFragment extends BaseFragment implements IChargeModeNotificationView {
    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    private ToggleButton charge_notification_box_id;
    @FindViewById
    private ProgressLayout cmn_progress;

    private ChargeModeNotificationPresenter presenter;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    /**
     * (non-Javadoc)
     * <p>
     * Title: onActivityCreated
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @param savedInstanceState
     * @see BaseFragment#onActivityCreated(Bundle)
     */
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isFirstLoad()) {
//            top_layout.setLeftButtonClickListener(new OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    back();
//                }
//            });
            presenter = new ChargeModeNotificationPresenter(((BaseActivity) getActivity()).getScheduler());
            presenter.setView(this);
            presenter.loadChargeMode();
            initView();
        }
    }


    @Override
    public void setLoading(boolean isLoading) {
        cmn_progress.setVisibility(isLoading ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onResponseError(OnStarException e) {
        ToastView toast = new ToastView(getActivity());
        toast.showToast(getString(R.string.option_get_cn_failed));
        back();
    }

    @Override
    public void onReponseSuccess(Class<?> object) {

    }

    @Override
    public Activity onActivity() {
        return getActivity();
    }



    private void initView() {
        charge_notification_box_id.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                charge_notification_box_id.setSelected(!charge_notification_box_id.isSelected());
                presenter.putChargeMode(!charge_notification_box_id.isSelected());
            }
        });
    }


    /**
     * (non-Javadoc)
     * <p>
     * Title: getLayoutId
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @return
     * @see BaseFragment#getLayoutId()
     */
    @Override
    public int getLayoutId() {
        return R.layout.discover_charge_mode_notification;
    }

    /**
     * (non-Javadoc)
     * <p>
     * Title: onDestroy
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @see android.support.v4.app.Fragment#onDestroy()
     */

    @Override
    public void onDestroy() {
        super.onDestroy();
        top_layout = null;
        presenter.finish();
    }

    @Override
    public void onLoadMode(String json) {
        if(!TextUtils.isEmpty(json) && json.contains("true")){
            charge_notification_box_id.setSelected(true);
        }else if(!TextUtils.isEmpty(json) && json.contains("false")){
            charge_notification_box_id.setSelected(false);
        }else{
            ToastView toast = new ToastView(getActivity());
            toast.showToast(getString(R.string.option_get_cn_failed));
            back();
        }
    }

    @Override
    public void onPutMode(String json, boolean close) {
        try {
            if (!TextUtils.isEmpty(json) && json.contains("E0000")) {
                ToastView toast = new ToastView(getActivity());
                toast.showToast(getString(R.string.option_put_cn_success));
            } else {
                ToastView toast = new ToastView(getActivity());
                toast.showToast(getString(R.string.option_put_cn_failed));
                charge_notification_box_id.setSelected(!charge_notification_box_id.isSelected());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPutFailed(boolean close) {
        ToastView toast = new ToastView(getActivity());
        toast.showToast(getString(R.string.option_bad_connection));
        charge_notification_box_id.setSelected(!charge_notification_box_id.isSelected());
    }
}
